package com.kakaxi.mybilibili.module.all;

import android.graphics.Bitmap;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.kakaxi.mybilibili.R;
import com.kakaxi.mybilibili.base.NavigationActivity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 公用的网页活动类
 * Created by kakaxi on 16/11/10.
 */

public class WebViewActivity extends NavigationActivity {

    /** 网页控件 **/
    @BindView(R.id.web_all_content)
    WebView mWebView;

    /** 网页读取进度条 **/
    @BindView(R.id.progress_all_loading)
    ProgressBar mWebLoadingProgress;

    /** 标题活动参数传递 **/
    public static String EXTRA_TITLE = "title";
    /** 网址活动参数传递 **/
    public static String EXTRA_URL = "url";

    /** 指定标题 **/
    private String mOrderTitle;

    @Override
    protected int getCurrentMenuItemId() {
        return R.menu.menu_nav;
    }

    @Override
    protected int getContentViewId() {
        return R.layout.activity_all_webview;
    }

    @Override
    protected void setupViews() {
        ButterKnife.bind(this);
        showBackBtn();
        initWebView();
    }

    @Override
    protected void initData() {
        mOrderTitle = getIntent().getStringExtra(EXTRA_TITLE);
        String url = getIntent().getStringExtra(EXTRA_URL);
        if (mOrderTitle != null) {
            setTitle(mOrderTitle);
        }
        if (url != null) {
            mWebView.loadUrl(url);
        }
    }

    /**
     * 初始化网页控件
     */
    private void initWebView() {
        // 设置支持js加载
        mWebView.getSettings().setJavaScriptEnabled(true);
        // 设置支持网页自适应
        mWebView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebView.getSettings().setLoadWithOverviewMode(true);
        // 设置网页加载进度
        mWebView.setWebChromeClient(new WebChromeClient() {
            @Override
            public void onProgressChanged(WebView view, int newProgress) {
                super.onProgressChanged(view, newProgress);
                // 显示当前页面标题
                showHtmlTitle();
                // 设置加载进度
                mWebLoadingProgress.setProgress(newProgress);
            }



        });
        // 设置网页
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }

            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                // 显示进度条
                mWebLoadingProgress.setVisibility(View.VISIBLE);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                // 隐藏进度条
                mWebLoadingProgress.setVisibility(View.INVISIBLE);
            }


        });
        // 后退处理
        mWebView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK && mWebView.canGoBack()) {
                    mWebView.goBack();
                    return true;
                }
                return false;
            }
        });
    }

    /**
     * 显示html标题
     */
    private void showHtmlTitle() {
        if (mOrderTitle == null) {
            String htmlTitle = mWebView.getTitle();
            if (htmlTitle != null && !htmlTitle.equals(""))
            setTitle(htmlTitle);
        }
    }

}
